import {
  Column,
  Entity,
  Index,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './User';
import { Collection } from './Collection';

@Index('user_id', ['userId'], {})
@Entity('article', { schema: 'blog_back' })
export class Article {
  @PrimaryGeneratedColumn({ type: 'int', name: 'id', unsigned: true })
  id: number;

  @Column('int', {
    primary: true,
    name: 'user_id',
    comment: '作者',
    unsigned: true,
  })
  userId: number;

  @Column('varchar', {
    name: 'filename',
    comment: '相对系统存储的文件名',
    length: 127,
  })
  filename: string;

  @Column('json', { name: 'tag_ids', comment: '标签的JSON array' })
  tagIds: object;

  @Column('varchar', { name: 'headline', comment: '标题', length: 63 })
  headline: string;

  @Column('varchar', {
    name: 'outline',
    nullable: true,
    comment: '摘要',
    length: 127,
  })
  outline: string | null;

  @Column('varchar', {
    name: 'cover_url',
    nullable: true,
    comment: '封面',
    length: 255,
  })
  coverUrl: string | null;

  @Column('enum', {
    name: 'status',
    comment: '文章是否有用',
    enum: ['1', '0'],
    default: () => "'1'",
  })
  status: '1' | '0';

  @Column('timestamp', {
    name: 'create_time',
    comment: '创建时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createTime: Date;

  @Column('timestamp', {
    name: 'update_time',
    comment: '更新时间',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateTime: Date;

  @ManyToOne(() => User, (user) => user.articles, {
    onDelete: 'NO ACTION',
    onUpdate: 'NO ACTION',
  })
  @JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
  user: User;

  @OneToMany(() => Collection, (collection) => collection.article)
  collections: Collection[];
}
